package com.example.mqltest.service.serviceImp;


import com.example.mqltest.dao.CourseDao;
import com.example.mqltest.dao.StudentDao;
import com.example.mqltest.dao.TeacherDao;
import com.example.mqltest.entity.*;
import com.example.mqltest.service.AccountService;
import com.example.mqltest.service.CourseService;
import com.example.mqltest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImp implements StudentService {
    @Autowired
    StudentDao studentDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private TeacherDao teacherDao;
    @Override
    public Result registerStudent(Student student) {
        if(studentDao.getStudentById( student.getStudentId() )!=null || teacherDao.getTeacherById( student.getStudentId() )!=null){
            return new Result(400,"注册失败，原因是ID已存在","");
        }
        if(studentDao.getStudentByPhoneNum( student.getPhoneNum() )!=null || teacherDao.getTeacherByPhoneNum( student.getPhoneNum() )!=null){
            return new Result(400,"注册失败，原因是手机号邮箱已存在","");
        }

        try {
            studentDao.register(student);
            return new Result(200,"注册成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙,注册失败","");
        }
    }

    @Override
    public Result exitCourse(String studentId, String courseCode) {
        try {
            studentDao.exitCourse(studentId,courseCode);
            return new Result(200,"退课成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，退课失败","");
        }
    }

    @Override
    public Result joinCourse(String studentId, String courseCode) {

        //判断是否已经加入课程
        if(courseDao.isJoin(studentId,courseCode)!=null){
            return new Result(404,"你已经加入了该课程","");
        }
        //判断是否存在该课程
        if(courseDao.getCourseByCode(courseCode)==null){
            return new Result(404,"课程码错误","");
        }
        try {
            studentDao.joinCourse(studentId,courseCode);
            return new Result(200,"加课成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，加课失败","");
        }
    }

}
